import json

from zhipuai import ZhipuAI

from llm.SystemConfig import SystemConfig


def get_response(message):
    client = ZhipuAI(api_key=SystemConfig().ZP_API_KEY)  # 请填写您自己的APIKey
    response = client.chat.completions.create(
        model=SystemConfig().ZP_MODULE,  # 填写需要调用的模型编码
        messages=[
            {"role": "system", "content": "你是一个乐于解答各种问题的助手，你的任务是为用户提供专业、准确、有见地的建议。"},
            {"role": "user", "content": message},
        ],
        stream=True,
    )

    content = ""  # 把数据进行累加
    for chunk in response:
        content += chunk.choices[0].delta.content
        # yield chunk.choices[0].delta.content
        yield "data: " + json.dumps({"type": "message", "content": chunk.choices[0].delta.content},
                                    ensure_ascii=False) + "\n\n"
        # print(chunk.choices[0].delta)
